#include <iostream>
using namespace std;

int a[400005], pre[200005];
long long s[400005], dp[400005];

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n * 2; i++) {
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
        if (pre[a[i]] == 0)
            pre[a[i]] = i;
    }
    long long mx = 0;
    for (int i = 1; i <= n * 2; i++)
        dp[i] = max(dp[i - 1], pre[a[i]] == i ? 0 : s[i] - s[pre[a[i]] - 1] + dp[pre[a[i]] - 1]), mx = max(mx, dp[i]);
    cout << mx << '\n';

    return 0;
}